tm.hotelMapping = {
	countryCode: function()
	{
		return $('#selector-country > input[name="cn"]').val();
	},
	cityId:  function()
	{
		return $('#city-selector > input[name="ct"]').val();
	},
	hotelCodes: function() {
		var hotelCodes = [];
		$('.search_results .search_results_item .system-hotels-search .lnk-system-hotel-search').each(function() {
			hotelCodes.push($(this).attr('name'));
		});
		return hotelCodes;
	},
	documentLoadAjax: function() {
		$.ajax({
			url: '/a_admin/hotel/mapping.getMappedHotels',
			data: {'hotelCodes[]': tm.hotelMapping.hotelCodes()},
			dataType: "json",
			type: "POST",
			success: function(data) {
				if (data.status == 'success') {
					tm.hotelMapping.setChecked(data.data);
				}
			},
			error: function() {
				alert('getMappedHotels error');
			}
		});
	},
	documentLoadSystemHotels: function() {
		
	},
    init: function()
    {
		function hotelMappingShow() {
			if ($('#hotel-mapping').is(':hidden')) {
				$('#hotel-mapping').show();
			}
		}
		
		tm.hotelMapping.documentLoadAjax();

		//$('#hotel-mapping').css('maxHeight', '800px');
		tm.hotelMapping.lnkSystemHotelSearchClick();
		$('form.search-controls').submit(function() {
			//$('.search_results .search_results_item .mapping').hide();
			//$('.search_results .search_results_item .system-controls').hide();
			var $this = $(this);
			var btnSubmit = $this.find('input.submit'), btnText = $this.find('input.text');
			var hotelName = btnText.val();
			if (btnText.val() != '') {
				$.ajax({
					url: '/a_admin/hotel/mapping.searchSystemHotelByString',
					data: {countryCode: tm.hotelMapping.countryCode, cityId: tm.hotelMapping.cityId, hotelName: hotelName},
					dataType: "json",
					type: "POST",
					beforeSend: function() {
						btnSubmit.hide().after('<img class="ajax-loader" src="/i/ajax-loader-small.gif" width="22" height="22" alt="" />');
						tm.hotelMapping.lnkSystemHotelSearchUnbindClick();
						tm.hotelMapping.systemHotelUnBindClick();
						tm.hotelMapping.unbindCheckboxClick();
					},
					success: function(data) {
						btnSubmit.show();
						$this.find('img.ajax-loader').remove();
						$('.search_results .system-hotels-search a.lnk-system-hotel-search').css('opacity', 1);
						if (data.status == 'success' && data.data.length > 0) {
							var responseData = '';
							for (var i = 0; i < data.data.length; i++) {
								var grade = data.data[i].grade == 0 ? 'NO_HOTEL_CLASS'.translate() : 'HOTEL_CLASS'.translate() + ': ' + data.data[i].grade + '*';
								responseData += '<li><a href="javascript:void(0)" title="' + grade + '" name="' + data.data[i].code + '">' + data.data[i].name + '</a></li>'
							}
							$('#hotel-mapping ul').html(responseData);
							tm.hotelMapping.systemHotelBindClick(tm.hotelMapping.hotelCodes());
							if ($('#hotel-mapping ul li').size() == 1) {
								$('#hotel-mapping ul li a').trigger('click');
							}
						} else if (data.status == 'success' && data.data.length <= 0) {
							$('#hotel-mapping ul').html('NO_SYSTEM_HOTELS'.translate());
							tm.hotelMapping.documentLoadAjax();
						} else if (data.status == 'error') {
							$('#hotel-mapping ul').html('SYS_HOTELS_SEARCH_ERROR'.translate());
							tm.hotelMapping.documentLoadAjax();
						}
						hotelMappingShow();
						tm.hotelMapping.lnkSystemHotelSearchClick();
						tm.hotelMapping.bindCheckboxClick();
					},
					error: function() {
						$('.search_results .system-hotels-search a.lnk-system-hotel-search').css('opacity', 1);
						btnSubmit.show();
						$this.find('img.ajax-loader').remove();
						$('#hotel-mapping ul').html('SYS_HOTELS_SEARCH_ERROR'.translate());
						hotelMappingShow();
						tm.hotelMapping.lnkSystemHotelSearchClick();
					}
				});
			} else {
				alert('INPUT_HOTEL_NAME'.translate());
			}
		});
    },
	lnkSystemHotelSearchClick: function()
	{
		$('.search_results .system-hotels-search a.lnk-system-hotel-search').bind('click', tm.hotelMapping.systemHotelSearch);
	},
	systemHotelSearch: function() {
		$(this).css('opacity', 0.5);
		$('form.search-controls input.text').val($(this).parents('.search_results_item').find('h3 a').text());
		$('form.search-controls').submit();
	},
	lnkSystemHotelSearchUnbindClick: function()
	{
		$('.search_results .system-hotels-search a.lnk-system-hotel-search').unbind('click', tm.hotelMapping.systemHotelSearch);
	},
	systemHotelBindClick: function(hotelCodes)
	{
		$('#hotel-mapping ul li a').bind('click', {hotelCodes: hotelCodes}, tm.hotelMapping.systemHotelClick);
	},
	systemHotelClick: function(event)
	{
		if ($('a.mapped-target').length > 0) {
			$('a.mapped-target').unbind('click', tm.hotelMapping.bindMappedTargetClick);
		}
		$('img.not-mapped').hide();
		var $this = $(event.target);
		if (!$this.hasClass('clicked')) {
			if ($('.search_results .search_results_item .mapping').is(':hidden')) {
				$('.search_results .search_results_item .mapping').show();
				tm.hotelMapping.bindCheckboxClick();
			}
			$this.parents('ul').find('a').removeClass('clicked');
			$this.addClass('clicked');
			//var systemHotelCode = $this.attr('name');
			$.ajax({
				url: '/a_admin/hotel/mapping.getMappedHotels',
				data: {'hotelCodes[]': event.data.hotelCodes},
				dataType: "json",
				type: "POST",
				beforeSend: function() {
					$this.append('<img class="ajax-loader" style="margin-left:10px; vertical-align:middle;" src="/i/ajax-loader-small.gif" width="14" height="14" alt="" />');
				},
				success: function(data) {
					$this.find('img.ajax-loader').remove();
					if (data.status == 'success') {
						tm.hotelMapping.setChecked(data.data);
					} else {
						alert('Error');
					}
				},
				error: function() {
					$this.find('img.ajax-loader').remove();
				}
			});
		}
	},
	systemHotelUnBindClick: function()
	{
		$('#hotel-mapping ul li a').unbind('click', tm.hotelMapping.systemHotelClick);
	},
	setChecked: function(arr)
	{
		$('.search_results .search_results_item').each(function(i) {
			var $this = $(this);
			var $thisChckbx = $this.find('.mapping input[type="checkbox"]');
			var vCode = $this.find('.system-hotels-search a.lnk-system-hotel-search').attr('name');
			var sCode = $('#hotel-mapping li a.clicked').attr('name');
			for (var j = 0; j < arr.length; j++) {

				if (arr[j].v == vCode) {
					$thisChckbx.siblings('label').find('span').text($('#hotel-mapping ul li a.clicked').text());
					if (arr[j].s == 0 || arr[j].s == '0') {
						$thisChckbx.attr('checked', false).next(0).find('img').removeClass('checked');
						if ($thisChckbx.parents('.mapping').siblings('img.not-mapped').is(':hidden')) {
							$thisChckbx.parents('.mapping').siblings('img.not-mapped').show();
						} else {
							$thisChckbx.parents('.mapping').siblings('img.not-mapped').remove();
							$thisChckbx.parents('.mapping').after('<img class="not-mapped" src="/i/icons/alert_icon.gif" width="32" height="32" alt="" />');
						}
						tm.hotelMapping.createSystemHotel($thisChckbx);
					} else if (sCode != undefined && arr[j].s == sCode) {
						$thisChckbx.attr('checked', 'checked').next(0).find('img').addClass('checked');
						$thisChckbx.parents('.mapping').siblings('.system-controls').hide();
					} else {
						var grade = arr[j].g == 0 ? 'NO_HOTEL_CLASS'.translate() : 'HOTEL_CLASS'.translate() + ': ' + arr[j].grade + '*';
						$thisChckbx.parents('.mapping').html('ATTACHED_TO'.translate() + ' <a href="javascript:void(0)" class="mapped-target" title="' + grade + '">' + arr[j].n + '</a>').show();
					}
				}
				

				if (arr[j].s == arr[j].v && arr[j].v == $this.find('.item_desc h3 a').metadata().index) {
					var sContent = '<ul class="menu hotels-mapped-to">';
					for (var k = 0; k < arr[j].m.length; k++) {
						var name = arr[j].m[k].n.length > 0 ? arr[j].m[k].n : 'NO_DATA'.translate();
						sContent += '<li><input id="m_'+ j + '_' + k + '" type="checkbox" checked="checked" value="' + arr[j].m[k].v + '" /> <label for="m_'+ j + '_' + k + '">' + name + ' (<span>' + arr[j].m[k].v + '</span>)</label></li>';
					}
					sContent += '</ul>';
					$this.find('.system-hotels-search').html(sContent);
					$this.find('.hotels-mapped-to input[type="checkbox"]').each(function() { tm.FormControls.replaceCheckbox(this); $(this).bind('change', tm.hotelMapping.checkboxFn); });
				}

			}
			
		});
		
		$('a.mapped-target').bind('click', tm.hotelMapping.bindMappedTargetClick);
		
	},
	createSystemHotel: function(elm) {
		var c = elm.parents('.system-hotels-search').find('.system-controls');
		if (c.length > 0 && c.is(':hidden')) {
			c.show();
		} else if (c.length <= 0) {
			elm.parents('.system-hotels-search').append('<span class="system-controls"><a href="javascript:void(0)">' + 'CREATE_SYSTEM_HOTEL'.translate() + '</a></span>');
			elm.parents('.system-hotels-search').find('.system-controls a').bind('click', tm.hotelMapping.createSystemHotelClick);
		}
	},
	createSystemHotelClick: function(e)
	{
		var e = e.target;
		var name = $(e).parents('.search_results_item').find('h3 a').text();
		var grade = $(e).parents('.search_results_item').find('.hotel_stars div').width() / 20;
		var hotelCode = $(e).parents('.search_results_item').find('.system-hotels-search a.lnk-system-hotel-search').attr('name');
		if (confirm('CREATE_SYSTEM_HOTEL'.translate() + '?')) {
			$.ajax({
				url: '/a_admin/hotel/mapping.createSystemHotel',
				data: {countryCode: tm.hotelMapping.countryCode, cityId: tm.hotelMapping.cityId, name: name, grade: grade, hotelCode: hotelCode},
				dataType: "json",
				type: "POST",
				success: function(data) {
					if (data.status == 'success') {
						$('form.search-controls > input.text').val('"' + name + '"');
						$('form.search-controls').submit();
					} else {
						alert('COULDNT_CREATE_SYS_HOTEL'.translate());
					}
				},
				error: function() {
					alert('REQUEST_SEND_ERROR'.translate());
				}
			});
		}
	},
	bindMappedTargetClick: function()
	{
		$('form.search-controls input.text').val('"' + $(this).text() + '"');
		$('form.search-controls').submit();
	},
	bindCheckboxClick: function()
	{
		$('.search_results .search_results_item .mapping input[type="checkbox"]').bind('change', tm.hotelMapping.checkboxFn);
	},
	checkboxFn: function()
	{
		var $this = $(this);
		var chckVal = $this.is(':checked') ? 1 : 0;
		var sysHotelCode = $('#hotel-mapping li a.clicked').attr('name');
		var hotelCode = $this.parents('.system-hotels-search').find('a.lnk-system-hotel-search').attr('name');
		var hotelGrade = $this.parents('.search_results_item').find('.hotel_stars div').width() / 20;
		if ($this.parents('ul').hasClass('hotels-mapped-to')) {
			if (hotelCode == undefined || hotelCode == '')
				hotelCode = $this.val();
			if (sysHotelCode == undefined || sysHotelCode == '')
				sysHotelCode = $this.parents('.search_results_item').find('h3 a').metadata().index;
		}
		function chckError(data) {
			if (data.status != 'success') {
				if ($this.is(':checked')) {
					$this.attr('checked', false).next(0).find('img').removeClass('checked');
				} else {
					$this.attr('checked', 'checked').next(0).find('img').addClass('checked');
				}
			}
			$this.parent(0).css('opacity', 1);
			var sc = $this.parents('.system-hotels-search').find('.system-controls');
			if ($this.is(':checked')) {
				$this.parents('.mapping').siblings('img.not-mapped').hide();
				if (sc.length > 0 && sc.is(':visible')) {
					sc.hide();
				}
			} else {
				if ($this.parents('.mapping').siblings('img.not-mapped').is(':hidden')) {
					$this.parents('.mapping').siblings('img.not-mapped').show();
				} else if ($this.parents('.mapping').siblings('img.not-mapped').length <= 0) {
					$this.parents('.mapping').after('<img class="not-mapped" src="/i/icons/alert_icon.gif" width="32" height="32" alt="" />');
				}
				if (sc.length > 0 && sc.is(':hidden')) {
					sc.show();
				} else if (sc.length <= 0 && !$this.parents('ul').hasClass('hotels-mapped-to')) {
					$this.parents('.system-hotels-search').append('<span class="system-controls"><a href="javascript:void(0)">' + 'CREATE_SYSTEM_HOTEL'.translate() + '</a></span>');
					$this.parents('.system-hotels-search').find('.system-controls a').bind('click', tm.hotelMapping.createSystemHotelClick);
				}
			}
		}
		$.ajax({
			url: '/a_admin/hotel/mapping.mapHotel',
			data: {countryCode: tm.hotelMapping.countryCode, cityId: tm.hotelMapping.cityId, sysHotelCode: sysHotelCode, hotelCode: hotelCode, map: chckVal, grade: hotelGrade},
			dataType: "json",
			type: "POST",
			beforeSend: function() {
				$this.parent(0).css('opacity', 0.5);
			},
			success: chckError,
			error: chckError
		});
	},
	unbindCheckboxClick: function()
	{
		$('.search_results .search_results_item .mapping input[type="checkbox"]').unbind('change', tm.hotelMapping.checkboxFn);
	}
};

$(tm.hotelMapping.init);